8630. Maximum digit in a number
Find the
maximum digit in a positive integer n.
Input. One positive integer n (n
< 1018
).
Output. Print the
maximum digit in the positive integer n.
Sample
input |
Sample
output |
2354 |
5 |
loop
Read the input value of n.
scanf("%lld", &n);
Find the maximum digit of the number n in the variable res.
res
= 0;
Iterate through all the digits of n. To do this, repeatedly divide n
by 10 and extract its last digit d = n % 10. Among all such
digits d, determine the maximum value.
while (n > 0)
{
d = n % 10;
if (d
> res) res = d;
n = n / 10;
}
Print the answer.
printf("%d\n", res);
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner con = new Scanner(System.in);
long n = con.nextLong();
long res = 0;
while (n > 0)
{
long d = n %
10;
if (d > res) res = d;
n = n / 10;
}
System.out.println(res);
con.close();
}
}
Read the input number as a string.
s = input()
The max() function returns the maximum character in the string.
· If the string consists only of digits, max() finds
the largest digit (for example, for “534543”, the result will be “5”).
· If the string contains letters, the characters are compared
in lexicographical order.
Print the answer.
print(max(s))